﻿using System;
using MathematicallySafe.Armourer.Base;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Models;
using MathematicallySafe.HeadCounter.Services;
using StructureMap;

namespace MathematicallySafe.HeadCounter.ViewModels
{
    public class HeadCountLootViewModel : ViewModelBase
    {
        private readonly IHeadCounterService _headCounterService = ObjectFactory.GetInstance<IHeadCounterService>();

        private int _id;
        private bool _ignore = false;
        private bool _downloading;
        private LootItem _item;
        private string _looterName;

        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;

                if (_headCounterService.LootTable.HasItem(_id))
                {
                    _item = _headCounterService.LootTable[_id];
                }
                else
                {
                    Downloading = true;
                    _headCounterService.LootTable.ItemDownloaded += LootTableItemDownloaded;

                    _headCounterService.LootTable.DownloadItemAsynch(_id, false);
                }
            }
        }

        void LootTableItemDownloaded(object sender, ItemDownloadedEventArgs e)
        {
            if (e.Id == _id)
            {
                _item = _headCounterService.LootTable[_id];

                _headCounterService.LootTable.ItemDownloaded -= LootTableItemDownloaded;
                Downloading = false;
            }
        }

        public string Name { get; set; }
        public string Boss { get; set; }
        public DateTime Time { get; set; }
        public int Cost { get; set; }
        public string LootType { get; set; }
        public string LootSubType { get; set; }

        public string LooterName
        {
            get { return _looterName; }

            set
            {
                _looterName = value;
                OnPropertyChanged("LooterName");
            }
        }

        public Uri LootUri
        {
            get { return _item != null ? _item.LootUri : new Uri("http://www.wowhead.com"); }
        }

        public bool Ignore
        {
            get { return _ignore; }
            set
            {
                _ignore = value;
                OnPropertyChanged("Ignore");
            }
        }

        public bool IsKnown
        {
            get { return _item != null; }
        }

        public bool Downloading
        {
            get { return _downloading; }
            set
            {
                _downloading = value;
                OnPropertyChanged("Downloading");
            }
        }

        public string Zone
        {
            get
            {
                if (IsKnown)
                {
                    return _item.Zone;
                }

                return "";
            }
        }

        public bool IsHeroic(bool TwentyFiveManRaid)
        {
            if (IsKnown)
            {
                // todo generalise this to cope with future raid zone
                if (TwentyFiveManRaid)
                {
                    return _item.iLevel >= 258;
                }
                else
                {
                    return _item.iLevel >= 245;
                }
            }

            return false;
        }
    }
}